package cc.fs.video.myvideoproject.ui.adapter

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import cc.fs.video.myvideoproject.R
import cc.fs.video.myvideoproject.beans.VideoBean
import cc.fs.video.myvideoproject.utils.TimeUtil
import cc.fs.video.myvideoproject.utils.bindView
import java.util.*

/**
 * Created by fostion on 2015/10/16.
 */
class VideoAdapter(private val videos:ArrayList<VideoBean>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var onClick:((VideoBean) -> Unit) ? = null

    override fun getItemCount(): Int {
        return videos.size()
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder? {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.item_video, parent, false)
        return VideoViewHolder(view)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        if(holder is VideoViewHolder){
           holder.setData(videos.get(position))
        }
    }

    private inner class VideoViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val title: TextView by bindView(R.id.title)
        val time: TextView by bindView(R.id.time)
        fun setData(video: VideoBean) {
            title.text = video.title
            time.text = TimeUtil.formatVideoTime(video.duration.toInt())

            itemView.setOnClickListener { onClick?.invoke(video) }
        }
    }

}